package com.dense.kuiniu.bookborrow.view.dialog;

import android.content.Context;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.serialport.entity.InventoryBean;
import com.dense.kuiniu.bookborrow.R;
import com.dense.kuiniu.bookborrow.view.CountDownView;
import com.dense.kuiniu.bookborrow.view.CounterController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @date 2020/8/11
 */
public class PutCardDialog extends DialogMeasure {
    private TextView mTvMsg;
    private OnCardReadListener mOnCardReadListener;
    private CountDownView mCountDownView;
    private EditText mEtCardNo;
    private boolean hasRead = false;
    private Handler mHandler;
    private Runnable mRunnable;
    private Map<String, InventoryBean> mReadMap = new HashMap<>();

    public PutCardDialog(Context context) {
        super(context);
    }


    @Override
    protected int layoutRes() {
        return R.layout.dialog_put_card;
    }

    @Override
    protected void init(Context context, View view) {
        view.findViewById(R.id.tv_exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit();
            }
        });
        mHandler = new Handler();
        mRunnable = new Runnable() {
            @Override
            public void run() {
                String cardNo = mEtCardNo.getText().toString().replace("\n", "").trim();
                if ("0684601403".equals(cardNo)) {
                    cardNo = "CS0001";
                }
                exit();
                if (mOnCardReadListener != null) {
                    mOnCardReadListener.onComplete(cardNo.trim());
                }
            }
        };

        mTvMsg = view.findViewById(R.id.tv_msg);
        mEtCardNo = view.findViewById(R.id.et_card_no);
        mEtCardNo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mRunnable != null) {
                    mHandler.removeCallbacks(mRunnable);
                    mHandler.postDelayed(mRunnable, 200);
                }
            }
        });
        mCountDownView = view.findViewById(R.id.count_down_view);
        mCountDownView.start(new CounterController.OnCompleteListener() {
            @Override
            public void onComplete() {
                exit();
            }
        });

        setCanceledOnTouchOutside(false);
    }



    public PutCardDialog msg(String msg) {
        if (mTvMsg != null) {
            mTvMsg.setText(msg);
        }
        return this;
    }

    public PutCardDialog cardListener(OnCardReadListener listener) {
        mOnCardReadListener = listener;
        return this;
    }


    @Override
    public float widthPercent() {
        return 0.78f;
    }

    @Override
    public float heightPercent() {
        return 0.31f;
    }

    private void exit() {
        if (mRunnable != null) {
            mHandler.removeCallbacks(mRunnable);
        }
        dismiss();
    }

    public interface OnCardReadListener {
        void onComplete(String cardNo);
    }
}